﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Collections;
using FooFun.DataElements.Dtos;

namespace PhysicalDataProvider
{
    public class PermissionSetService
    {
        string[] selectCols
            = new string[] { "AccountId", "PermissionId", "ForumId", "PermissionSet" };

        string table = "t_PermissionSets";

        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        public IDataElement ListPermissionSet()
        {
            IDataElement dbDE
                = DataProvider.Select(selectCols, table, null);

            SetListDataElement list = new SetListDataElement(dbDE);

            return list;
        }

        class SetDataElement : CustomDataElement
        {
            public SetDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "PermissionId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);
                        case "AccountId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);
                        case "ForumId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);            
                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }
        }

        class SetListDataElement : CustomDataElement
        {
            public SetListDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IEnumerator GetEnumerator()
            {
                foreach (IDataElement item in this.InnerDataElement)
                {
                    yield return new SetDataElement(item);
                }
            }
        }
    }
}
